package com.ipartek.formacion.gestformwww.controlador;

import com.ipartek.formacion.gestformwww.modelo.*;
import com.ipartek.formacion.gestformwww.modelo.beans.*;

import java.util.ArrayList;


import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Karmele & Borja
 *
 */
public class VisualizarMailAccion implements Accion {

    private final String visualizaralumno = "visualizar-alumno-accion";
    private final String visualizarcurso = "visualizar-curso-accion";
    private final String listarcursos = "listar-cursos-accion";

    /**
     * Muestra los mails enviados en un curso
     * @param servlet
     * @param request
     * @param response
     * @return
     */
    public AccionRouter ejecuta(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response) {
        HttpSession sesion;
        sesion = request.getSession();
        AccionRouter router = new AccionRouter("principal");
        AccionServlet as = (AccionServlet) servlet;
        String path = null;
        Integer idAlumno = -1;
        Integer idCurso = -1;

        ModeloCorreo modelo = (ModeloCorreo) servlet.getServletContext().getAttribute("ModeloCorreo");
        ModeloAlumno modeloAlumno = (ModeloAlumno) servlet.getServletContext().getAttribute("ModeloAlumno");

        ArrayList<Correo> listacorreo = null;


        if ((Verificador.numeroValido(request.getParameter("idAlumno")) || (request.getAttribute("idAlumno") != null))
                && (Verificador.numeroValido(request.getParameter("idCurso")) || (request.getAttribute("idCurso") != null))) {
            if (request.getAttribute("idAlumno") != null) {
                idAlumno = (Integer) request.getAttribute("idAlumno");
                request.setAttribute("idAlumno", idAlumno);
            } else {
                idAlumno = Integer.parseInt(request.getParameter("idAlumno"));
            }
            if (request.getAttribute("idCurso") != null) {
                idCurso = (Integer) request.getAttribute("idCurso");
                request.setAttribute("idCurso", idCurso);
            } else {
                idCurso = Integer.parseInt(request.getParameter("idCurso"));
            }


            request.setAttribute("listacorreo", listacorreo);
            request.setAttribute("contenido-alumno", "email-alumno");

        } else {
            try {
                router=as.getFactory().getAction(as.getNombreClaseRecurso(listarcursos),servlet.getClass().getClassLoader()).ejecuta(servlet, request, response);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(VisualizarMailAccion.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(VisualizarMailAccion.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(VisualizarMailAccion.class.getName()).log(Level.SEVERE, null, ex);
            }
           
        }

        return router;
    }

   
}
